package com.atguigu.gulimall.ware.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.gulimall.ware.vo.ItemVO;
import com.atguigu.gulimall.ware.vo.MergePurchaseVO;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 采购信息
 *
 * @author Shawn
 * @email 572484122@qq.com
 * @date 2021-03-27 11:17:19
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 完成采购
     *
     * @param purchaseDoneVO {
     *                       id: 123,//采购单id
     *                       items: [{itemId:1,status:4,reason:""}]//itemId：采购明细id;完成/失败的需求详情
     *                       }
     * @return
     */
    @PostMapping("/done")
    public R finishPurchase(@Valid @RequestBody PurchaseDoneVO purchaseDoneVO) {
        purchaseService.done(purchaseDoneVO);
        return R.ok();
    }

    /**
     * 领取采购单
     *
     * @param purchaseIds 采购单id
     * @return
     */
    @PostMapping("/received")
    public R receivedPurchase(@RequestBody List<Long> purchaseIds) {
        purchaseService.receivedPurchase(purchaseIds);
        return R.ok();
    }

    /**
     * 合并采购单
     * 访问路径：/ware/purchase/merge
     */
    @PostMapping("/merge")
    public R mergePurchase(@RequestBody MergePurchaseVO mergePurchaseVO) {
        purchaseService.mergePurchase(mergePurchaseVO);
        return R.ok();
    }

    /**
     * 未领取的采购的单
     * 访问路径：/ware/purchase/unreceive/list
     *
     * @return
     */
    @GetMapping("/unreceive/list")
    public R unreceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryUnreceivePage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
